﻿using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;

using Com.Ompchina.Lib.DBHelper;
using IDAL.Common;
using IDAL;
using Model.Common;
namespace SQLServerDAL.Common
{
    /// <summary>
    /// 数据层，通用版块，城市
    /// </summary>
    public class CityDAL : DalBase, ICity
    {
        public CityDAL() 
        {
            Schema = "cms.";
            ViewTableName = "V_City";
            Fields = "CityID, CityName, HasInfo, ProvinceID, ProvinceName";
        }

        #region ICity 成员
        /// <summary>
        /// 城市列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public IList<CityInfo> ListCity(CityCondition condition)
        {
            SqlBuilder = new StringBuilder("SELECT ");
            SqlBuilder.AppendFormat(" {0} FROM {1} WHERE 1 = 1 ", Fields, Schema + ViewTableName);
            if (condition.ProvinceID > 0)
                SqlBuilder.AppendFormat(" AND ProvinceID = {0}", condition.ProvinceID);
            if (!string.IsNullOrEmpty(condition.HasInfo))
                SqlBuilder.AppendFormat(" AND HasInfo = '{0}'", condition.HasInfo);
            SqlDataReader dr = SqlHelper.ExecuteReader(base.ConnectionString, CommandType.Text, SqlBuilder.ToString());
            List<CityInfo> list = new List<CityInfo>();
            if (dr != null)
            {//CityID, CityName, HasInfo, ProvinceID, ProvinceName
                while (dr.Read())
                {
                    int i = 0;
                    CityInfo info = new CityInfo();
                    info.CityID = dr.GetInt32(i++);
                    info.CityName = dr.GetString(i++);
                    info.HasInfo = dr.GetString(i++);
                    info.Province = new ProvinceInfo();
                    info.Province.ProvinceID = dr.GetInt32(i++);
                    info.Province.ProvinceName = dr.GetString(i++);
                    list.Add(info);
                }
                dr.Close();
            }
            return list;
        }

        #endregion
    }
}
